package com.hhdys.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hhdys.dao.WebSetMapper;
import com.hhdys.model.WebSet;
import com.hhdys.util.Constant;
@Service
public class WebSetServiceImpl implements WebSetService {
	
	private static Logger log=LogManager.getLogger(WebSetServiceImpl.class);
	@Autowired
	private WebSetMapper dao;
	@Override
	public List<WebSet> selectWebSets() {
		return dao.selectWebSets();
	}
	/**
	 * @return the dao
	 */
	public WebSetMapper getDao() {
		return dao;
	}
	/**
	 * @param dao the dao to set
	 */
	public void setDao(WebSetMapper dao) {
		this.dao = dao;
	}
	@Override
	public int addWebSet(WebSet webSet) {
		if(addOrEditInitWebset(webSet)){
			return dao.insert(webSet);
		}
		return 0;
	}
	@Override
	@Transactional(rollbackFor=Exception.class)
	public void delWebSet(String ids) {
		String[] idsStrs=ids.split(",");
		for(String str:idsStrs){
			dao.deleteByPrimaryKey(NumberUtils.toInt(str));
		}
		
		List<WebSet> list=selectWebSets();
		Constant.WEBSET.clear();//清除所有设置
		initWebSet(list);
	}
	@Override
	public int editWebSet(WebSet webSet) {
		if(addOrEditInitWebset(webSet)){
			return dao.updateByPrimaryKey(webSet);
		}
		return 0;
	}
	@Override
	public WebSet selectWebsetById(int id) {
		return dao.selectByPrimaryKey(id);
	}
	private Map<Integer, WebSet> getParentWebSetMap(){
		List<WebSet> list=selectParentWebSets();
		if(list==null){
			return null;
		}
		Map<Integer, WebSet> map=new HashMap<Integer, WebSet>();
		for(WebSet webSet:list){
			map.put(webSet.getId(), webSet);
		}
		return map;
	}
	
	@Override
	public void initWebSet(List<WebSet> list) {
		if(list==null){
			log.error("初始化属性表失败！list为空");
			return ;
		}
		Map<Integer, WebSet> parentMap=getParentWebSetMap();
		for(WebSet set:list){
	    	if(set.getParentKey()==0){//如果是父键
	    		if(Constant.WEBSET.containsKey(set.getKey())){//如果已经添加跳过
	    			continue;
	    		}
	    		Map<String, WebSet> webSetsMap=new HashMap<String, WebSet>();
	    		webSetsMap.put(set.getKey(), set);
	    		Constant.WEBSET.put(set.getKey(), webSetsMap);
	    	}else {//如果不是
	    		//首先获取此设置的父设置
	    		WebSet parentSet=parentMap.get(set.getParentKey());
	    		if(parentSet==null){
	    			continue;
	    		}
	    		Map<String, WebSet> childSets=Constant.WEBSET.get(parentSet.getKey());
	    		if(childSets==null){//如果没有子设置
	    			childSets=new HashMap<String,WebSet>();
	    		}
	    		childSets.put(set.getKey(), set);
	    		Constant.WEBSET.put(parentSet.getKey(), childSets);
			}
	    }
	}
	
	private boolean addOrEditInitWebset(WebSet webSet){
		if(webSet.getParentKey()==0){
			Map<String,WebSet> map=new HashMap<String,WebSet>();
			map.put(webSet.getKey(), webSet);
			Constant.WEBSET.put(webSet.getKey(), map);
		}else {
			//首先获取此设置的父设置
    		WebSet parentSet=selectWebsetById(webSet.getParentKey());
    		if(parentSet==null){
    			log.error("添加属性表获取父节点错误！");
    			return false;
    		}
    		Map<String, WebSet> childSets=Constant.WEBSET.get(parentSet.getKey());
    		if(childSets==null){//如果没有子设置
    			childSets=new HashMap<String, WebSet>();
    		}
    		childSets.put(webSet.getKey(), webSet);
    		Constant.WEBSET.put(parentSet.getKey(), childSets);
		}
		return true;
	}
	@Override
	public List<WebSet> selectParentWebSets() {
		return dao.selectParentWebSets();
	}
	@Override
	public String showParentSetJson(List<WebSet> parentSet) {
		if(parentSet==null){
			return "";
		}
		StringBuilder result=new StringBuilder("[{\"id\":0,\"text\":\"无\"}");
		for(WebSet set:parentSet){
			result.append(",{\"id\":"+set.getId()+",\"text\":\""+set.getKey()+"\"}");
		}
		result.append("]");
		return result.toString();
	}
}
